package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import bean.Menu;
import util.DBUtil;

/**
 * Servlet implementation class AccordionServlet
 */
@WebServlet("/servlet/getAccordion")
public class AccordionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		Connection conn=null;
		String sql="";
		try {
			QueryRunner queryRunner=new QueryRunner();
			conn=DBUtil.getConn();
			sql="select * from menu where grade=1";
			
			List<Menu> menuList=queryRunner.query(conn, sql, new BeanListHandler<>(Menu.class));
			request.setAttribute("menuList", menuList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		request.getRequestDispatcher("/systemLayout.jsp").forward(request, response);
		
	}

}
